package com.zzxy.spring3.factory;

import com.zzxy.spring3.aspect.MyAspect;
import com.zzxy.spring3.service.impl.IAccountService;
import com.zzxy.spring3.service.impl.IStudentService;
import com.zzxy.spring3.service.impl.StudentServicelmpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyBeanFactory {
    public static <StudentServiceimpl> IStudentService creatStudentServiceProxy() {

        StudentServiceimpl studentService = new StudentServiceimpl();

        MyAspect aspect = new MyAspect();
        IStudentService studentServiceProxy =(IStudentService) Proxy.newProxyInstance(MyBeanFactory.class.getClassLoader(), studentService.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                aspect.before();
                Object obj = method.invoke(studentService, args);
                aspect.after();
                return obj;
            }
        });
        return studentServiceProxy;
    }

}